var IMG = $(".cropper-box img")
IMG.cropper({
        //声明图片长宽比
        aspectRatio: 1,
        //声明预览区域
        preview: ".preview-img"
    })
    //选择图片
$(".choose-img").on("click", function() {
        $(".file").click();
    })
    //跟换到裁剪区域
$(".file").on("change", function(e) {
        console.log(e);
        var files = e.target.files;
        if (files.length === 0) {
            layer.msg("心里没点逼数？")
            return;
        }
        var imgURL = URL.createObjectURL(files[0]);

        IMG.cropper("destroy").attr("src", imgURL).cropper({
            //声明图片长宽比
            aspectRatio: 1,
            //声明预览区域
            preview: ".preview-img"
        })
    })
    //上传图片
$(".upload-img").on("click", function() {
    var base64 = IMG
        //获取裁剪的图片
        .cropper("getCroppedCanvas", {
            width: 100,
            height: 100
        })
        //转换成base64
        .toDataURL("image/png")

    $.ajax({
        url: "/my/update/avatar",
        method: "POST",
        data: {
            avatar: base64
        },
        success: function(res) {
            layer.msg(res.message)
            if (res.status === 0) {
                window.parent.getUserInfo()
            }
        }
    })
})